/*
 *  Slider.cpp
 *  Fridge
 *
 *  Created by Jeremy Abel on 7/7/12.
 *
 */

// Main header
#include "../Main.h"

// My Header
#include "Slider.h"

// Fridge
#include "../Controller/Controller_Main.h"
#include "../Input.h"
#include "../MathsHelper.h"

static const u_int uSLIDER_TRACK		= 0x000000FF;
static const u_int uSLIDER_BOX_WIDTH	= 16;
static const u_int uSLIDER_BOX_HEIGHT	= 20;

Slider::Slider( Widget *pxParent ) : Widget( pxParent )
{
	m_bShowScaleLines = true;
	m_bShowMinMaxLabels = true;
	m_bMouseDownState = false;
	m_fValue = 0.0f;
	m_fMinValue = 0.0f;
	m_fMaxValue = 1.0f;
	m_iMouseStartOffset = 0;
	m_iSliderDragStartOffset = 0;
	
	m_pxSliderButton = new Button( NULL );
	m_pxSliderButton->SetSize( uSLIDER_BOX_WIDTH, uSLIDER_BOX_HEIGHT );
}

Slider::~Slider()
{
	
}

void Slider::Render()
{
	 if( !m_bVisible )
	 {
		 return;
	 }
	
	// Draw line
	const float fPositionX = static_cast< float >( GetResizedX() );
	const float fPositionY = static_cast< float >( GetResizedY() );
	const float fWidth = static_cast< float >( GetResizedWidth() );
	const float fHeight = static_cast< float >( GetResizedHeight() );

	glBindTexture( GL_TEXTURE_2D, INVALID_TEXTURE_ID );
	glLineWidth( 0.5f );
	
	const u_int uSwappedBackColour = MathsHelper::RGBAtoABGR( uSLIDER_TRACK );
	glColor4ubv( reinterpret_cast<const GLubyte*>( &uSwappedBackColour ) );
	
	glBegin( GL_LINES );
	
	glVertex2f( fPositionX + ( uSLIDER_BOX_WIDTH / 2 ), fPositionY + ( fHeight / 2.0f ) );
	glVertex2f( fPositionX + fWidth - ( uSLIDER_BOX_WIDTH / 2 ), fPositionY + ( fHeight / 2.0f ) );
	
	glEnd();
	
	// TODO: Draw scale lines
	if( m_bShowScaleLines )
	{
	}
	
	m_pxSliderButton->Render();
}

void Slider::Update()
{	
	const Input* pxInput = Controller_Main::GetInputHandler();
	FAssert( pxInput != 0 );
	
	int iRemappedPos = (int)MathsHelper::Remap( m_fValue, m_fMinValue, m_fMaxValue, (float)uSLIDER_BOX_WIDTH / 2.0f, (float)m_uWidth - ( (float)uSLIDER_BOX_WIDTH / 2.0f ) );
	
	// Move the handle if we're dragging it
	if( m_bMouseDownState && pxInput->MouseMoved() )
	{
		int iMouseStartPoint = m_iSliderDragStartOffset - m_iMouseStartOffset;
		int iMouseDiff = pxInput->MousePositionX() - iMouseStartPoint;
	
		m_fValue = MathsHelper::Remap( (float)m_iSliderDragStartOffset + (float)iMouseDiff, 0.0f, (float)m_uWidth, m_fMinValue, m_fMaxValue );
		m_fValue = MathsHelper::Clamp( m_fValue, m_fMinValue, m_fMaxValue );

		m_bModified = true;
	}
	else if( pxInput->MouseButtonPressed( INPUT_MOUSE_BUTTON_LEFT ) && m_pxSliderButton->MouseInWidget() )
	{
		m_bMouseDownState = true;
		
		// Record intial state on click
		m_iMouseStartOffset = iRemappedPos - ( pxInput->MousePositionX() );
		m_iSliderDragStartOffset = iRemappedPos;
	}
	
	if( pxInput->MouseButtonReleased( INPUT_MOUSE_BUTTON_LEFT ) )
	{
		m_bMouseDownState = false;
	}
	
	m_pxSliderButton->SetPosition( ( GetResizedX() + (u_int)iRemappedPos ) - ( uSLIDER_BOX_WIDTH / 2 ), GetResizedY() + ( GetResizedHeight() / 2 ) - ( uSLIDER_BOX_HEIGHT / 2 ) );
	m_pxSliderButton->Update();
}

// eof
